package org.client.ui;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

import org.client.tools.ClientTool;
import org.client.view.manage.Client2ServerThread;
import org.client.view.manage.IMRegisterClientConServer;
import org.pojo.*;
import org.pojo.Message.*;

public class IMEditWnd extends JFrame implements ActionListener{
	private JPanel p ;
	private JLabel lblsign,lblnickname/*,lblpwd,lblRePwd*/,lblSex;
	private JTextField txtnickname,txtSign;
	//private JPasswordField txtpwd,txtRePwd;
	private JRadioButton rbMale,rbFemale;
	private JButton btnOK,btnReturn;
	public IMEditWnd(){
	super("编辑信息");
	p = new JPanel(null);
	lblnickname = new JLabel("昵称");
	lblsign = new JLabel("个性签名");
	//lblpwd = new JLabel("修改密码");
	//lblRePwd = new JLabel("确认密码");
	lblSex = new JLabel("性别");
	txtnickname = new  JTextField(20);
	//txtpwd = new JPasswordField(20);
	//txtRePwd =  new JPasswordField(20);
	rbMale = new  JRadioButton("男");
	rbMale.setSelected(true);
	rbFemale = new JRadioButton("女");
	txtSign = new JTextField(50);
	btnOK = new JButton("确定");
	btnOK.addActionListener(this);
	btnReturn = new JButton("取消");
	btnReturn.addActionListener(this);
	ButtonGroup bgsex = new ButtonGroup();
	bgsex.add(rbMale);
	bgsex.add(rbFemale);
	lblnickname.setBounds(30, 30, 60, 25);
	txtnickname.setBounds(95,30,200,25);
	lblsign.setBounds(30,60,60,25);
	txtSign.setBounds(95,60,200,25);
	//lblpwd.setBounds(30,90,60,25);
	//txtpwd.setBounds(95,90,200, 25);
	//lblRePwd.setBounds(30,120,60,25);
	//txtRePwd.setBounds(95, 120, 200, 25);
	lblSex.setBounds(30, 90, 60, 25);
	rbMale.setBounds(95,90, 60, 25);
	rbFemale.setBounds(160,90,60,25);
	btnOK.setBounds(70, 120, 60, 25);
	btnReturn.setBounds(200,120,60,25);
	p.add(lblnickname);
	p.add(txtnickname);
	//p.add(lblpwd);
	//p.add(txtpwd);
	p.add(lblsign);
	p.add(txtSign);
	//p.add(lblRePwd);
	//p.add(txtRePwd);
	p.add(lblSex);
	p.add(rbMale);
	p.add(rbFemale);
	p.add(btnOK);
	p.add(btnReturn);
	this.add(p);
	this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));
	this.setSize(350, 200);
	this.setLocation(300, 300);
	this.setResizable(false);
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IMEditWnd ui = new IMEditWnd();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if(source == btnOK)
		{
			try {
				btnOKClick();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			JOptionPane.showMessageDialog(btnOK, "信息已更改！", "成功", JOptionPane.INFORMATION_MESSAGE);
			this.dispose();
		}
		if(source == btnReturn)
		{
			this.dispose();
		}
	}
	private void btnOKClick() throws IOException
	{
		String nickname = txtnickname.getText();
		//String userpwd = new String(txtpwd.getPassword());
		//String repwd = new String(txtRePwd.getPassword());
		String sign = txtSign.getText();
		String sex;
		if(rbFemale.isSelected())
		{
			sex = "男";
		}
		else
		{
			sex = "女";
		}
		if(nickname.equals(""))
		{
			nickname = ClientTool.currentUser.getNickname();
		}
		/*
		if(userpwd.equals(""))
		{
			JOptionPane.showMessageDialog(btnOK, "密码不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(userpwd.length()<6||userpwd.length()>10)
		{
			JOptionPane.showMessageDialog(btnOK, "密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(repwd.equals(""))
		{
			JOptionPane.showMessageDialog(btnOK, "确认密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(!repwd.equals(userpwd))
		{
			JOptionPane.showMessageDialog(btnOK, "确认密码与密码必须相同","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		*/
		//注册提交
		User u = new User(nickname,sex,sign,null,ClientTool.currentUser.getUserid());
		Message m = new  Message();
		m.setMesType(MessageType.message_update_personinfo);
		m.setCon(u);
		Client2ServerThread.oos.writeObject(m);
	}

}
